<?php
class M_Choice extends CI_Model{
    private $table = 'movie_choice';
    var $id;
    var $movie_id;
    
    function __construct() {
        parent::__construct();
    }
    
    /**
     * lay danh sach tuyen chon
     * @return type movie array
     */
    function getChoiceMoviesList(){
        $this->db->cache_on();
        $sql = "SELECT m.movie_id, m.name, m.introduction, m.date, m.image, m.rating, m.actors, sum( ep.views ) as views, ch.id as id
                FROM movies m, movie_episode ep, movie_choice ch
                WHERE ep.movie_id = m.movie_id AND ch.movie_id = m.movie_id
                GROUP BY m.movie_id, m.name, m.introduction, m.date, m.image, m.rating, m.actors";
        $query = $this->db->query($sql);
        return $query->result_array();        
    }

    function addNew(){
        $data = array('movie_id'=>$this->movie_id);
        $this->db->insert($this->table, $data);
    }

    function doDelete($id){
        $this->db->where('id', $id);
        $this->db->delete($this->table);
    }

    /**
     * @param  $movieID
     * @return bool
     */
    function isExist(){
        $data = array('movie_id'=>$this->movie_id);
        $query = $this->db->get_where($this->table, $data);
        $result = $query->row_array();
        if(count($result)==0) return false;
        return true;
    }

}

?>
